var noPlanArray = ["corpSelectTR", "typeTR", "moneyTR", "shoukuanTR",
		"noPlanButton"];
var planArray = ["noTR", "dateTR", "queryButton"];
var planStore;
function $(name) {
	return document.getElementsByName(name);
}
function id(id) {
	return document.getElementById(id);
}
function $2(name) {
	return window.dialogArguments.document.getElementsByName(name);
}
function _ajax(url, operate) {
	window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
			- 3 + "px";
	window.parent.document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				var data = eval("(" + text + ")");
				if (data.success)
					operate(data);
				else
					alert(data.data)
			} else {
				alert("连接服务器异常");
			}
			window.parent.document.getElementById("lodingDiv").style.display = 'none';
		}
	};
	xmlhttp.open("get", url + "&temp=" + Math.random(), true);
	xmlhttp.send();
}
function createOption(msg,key){
	var op = document.createElement("option");
	op.value = key;
	op.text = msg;
	return op;
}
window.onload = function() {
	_ajax(	"../../oa2uapController.do?method=fundCorpPlan&operate=1&baoxiao="
					+ (($2("my:报销类型") && $2("my:报销类型").length > 0)
							? true
							: false), function(data) {
				if (data.cost) {
					for (var i = 0; i < data.cost.length; i++) {
						id("typeSelect").options.add(createOption(
								data.cost[i].name, data.cost[i].key));
					}
				}
				for (var i = 0; i < data.corp.length; i++) {
					id("corpSelect").options.add(createOption(data.corp[i].name,
							 data.corp[i].key));
				}
			});
	hideInput();
}

function hideInput() {
	if (!$2("my:报销类型") || $2("my:报销类型").length == 0) {
		id("typeTR").style.display = 'none';
	}
	if ($2("my:借款人") && $2("my:借款人").length > 0) {
		id("shoukuanTR").style.display = 'none';
	}
}

function onFundTypeSelectChange(select) {
	if (select.value == "key_0") {
		for (var i = 0; i < noPlanArray.length; i++) {
			id(noPlanArray[i]).style.display = '';
		}
		for (var i = 0; i < planArray.length; i++) {
			id(planArray[i]).style.display = 'none';
		}

		hideInput();

		id("showDiv").innerHTML = "";
		id("showDiv").style.display = "none";
		planStore = null;
	} else if (select.value == "key_1") {
		for (var i = 0; i < noPlanArray.length; i++) {
			id(noPlanArray[i]).style.display = 'none';
		}
		for (var i = 0; i < planArray.length; i++) {
			id(planArray[i]).style.display = '';
		}
		id("showDiv").style.display = "";
		id("showDiv").innerHTML = "";
		planStore = null;
	}
}

function onNoPlanButtonClick() {
	var money = id("money").value;
	if (money == "") {
		alert("报销金额不能为空");
		return;
	}
	if (isNaN(money)) {
		alert("报销金额必须为数字");
		return;
	}
	if (money.indexOf('.') != -1 && (money.length - money.indexOf('.')) > 3) {
		alert("报销金额的小数只能保留两位");
		return;
	}
	if (!$2("my:借款人") || $2("my:借款人").length == 0) {
		if (id("shoukuan").value.replace(/ /g, '') == '') {
			alert("收款人/收款单位不能为空");
			return;
		}
	}
	$2("my:金额小写")[0].value = money;
	$2("my:所属制造部")[0].value = id("corpSelect").value;
	$2("my:报账业务类型主键")[0].value = id("typeSelect").value;
	$2("my:所属制造部名")[0].value = id("corpSelect").options[id("corpSelect").options.selectedIndex].text;
	if ($2("my:报销类型") && $2("my:报销类型").length > 0) {
		$2("my:报销类型")[0].value = id("typeSelect").options[id("typeSelect").options.selectedIndex].text;
		$2("my:币种")[0].value = id("currencySelect").value;
	}

	if ($2("my:收款人") && $2("my:收款人").length > 0) {
		$2("my:收款人")[0].value = id("shoukuan").value;
	} else if ($2("my:收款单位") && $2("my:收款单位").length > 0) {
		$2("my:收款单位")[0].value = id("shoukuan").value;
	}
	$2("my:付款计划主键")[0].value = "NONE";
	
	window.dialogArguments.onkeyupColor($2("my:金额小写")[0], $2("my:金额小写")[0]);
	window.dialogArguments.calc($2("my:金额小写")[0]);
	window.close();
}
function writeToForm(index) {
	$2("my:金额小写")[0].value = planStore[index].plan_amount;
	$2("my:所属制造部")[0].value = planStore[index].pk_corp;
	$2("my:报账业务类型主键")[0].value = planStore[index].pk_costsubj;
	$2("my:所属制造部名")[0].value = planStore[index].unitname;
	if ($2("my:报销类型") && $2("my:报销类型").length > 0) {
		$2("my:报销类型")[0].value = planStore[index].costname;;
		$2("my:币种")[0].value = planStore[index].currtypecode;;
	}
	if ($2("my:收款人") && $2("my:收款人").length > 0) {
		$2("my:收款人")[0].value = planStore[index].opp_corp;
	} else if ($2("my:收款单位") && $2("my:收款单位").length > 0) {
		$2("my:收款单位")[0].value = planStore[index].opp_corp;
	}
	
	$2("my:付款计划主键")[0].value = planStore[index].pk_cash_plan;
	window.dialogArguments.onkeyupColor($2("my:金额小写")[0], $2("my:金额小写")[0]);
	window.dialogArguments.calc($2("my:金额小写")[0]);
	window.close();
}
function onQueryButtonClick() {
	planStore = null;
	_ajax(	"../../oa2uapController.do?method=fundCorpPlan&operate=2&no="
					+ id("bill_no").value.replace(/ /g, '') + "&begain="
					+ id("begain").value + "&end=" + id("end").value, function(
					data) {
				planStore = data.data;

				var rowArray = ["<table align='center'>"];
				rowArray.push("<tr valign=\"middle\" id='showTableHead'>");
				rowArray
						.push("<td style='width:80px;text-align:center;font: 黑体 bold;'>业务单号</td>");
				rowArray
						.push("<td style='width:100px;text-align:center;font: 黑体 bold;'>收款人/单位</td>");
				rowArray
						.push("<td style='width:120px;text-align:center;font: 黑体 bold;'>所属公司</td>");
				rowArray
						.push("<td style='width:90px;text-align:center;font: 黑体 bold;'>报账业务类型</td>");
				rowArray
						.push("<td style='width:90px;text-align:center;font: 黑体 bold;'>计划日期</td>");
				rowArray
						.push("<td style='width:40px;text-align:center;font: 黑体 bold;'>币种</td>");
				rowArray
						.push("<td style='width:80px;text-align:center;font: 黑体 bold;'>计划金额</td>");
				rowArray.push("<td></td>");
				rowArray.push("</tr>");
				for (var i = 0; i < data.data.length; i++) {
					rowArray.push("<tr valign='middle'>");
					rowArray.push("<td >");
					rowArray.push(data.data[i].bill_no);
					rowArray.push("</td>");

					rowArray.push("<td >");
					rowArray.push(data.data[i].opp_corp);
					rowArray.push("</td>");

					rowArray.push("<td >");
					rowArray.push((data.data[i].unitname));
					rowArray.push("</td>");

					rowArray.push("<td >");
					rowArray.push((data.data[i].costname));
					rowArray.push("</td>");

					rowArray.push("<td >");
					rowArray.push((data.data[i].plan_date));
					rowArray.push("</td>");

					rowArray.push("<td>");
					rowArray.push((data.data[i].currtypecode));
					rowArray.push("</td>");

					rowArray.push("<td >");
					rowArray.push((data.data[i].plan_amount));
					rowArray.push("</td>");

					rowArray.push("<td align='center'>");
					rowArray
							.push("<a onclick='writeToForm(" + i + ") '>选择</a>");
					rowArray.push("</td>");
					rowArray.push("</tr>");

				}
				rowArray.push("</table>")

				id("showDiv").innerHTML = rowArray.join("");
			});
}